//index.js  
//获取应用实例  
var app = getApp();
//引入这个插件，使html内容自动转换成wxml内容
var WxParse = require('../../wxParse/wxParse.js');
Page({
  firstIndex: -1,
  data: {
    imageUrlFix: app.d.imageUrlFix,
    bannerApp: true,
    winWidth: 0,
    winHeight: 0,
    currentTab: 0, //tab切换  
    productId: 0,
    itemData: {},
    bannerItem: [],
    buynum: 1,
    // 产品图片轮播
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    // 属性选择
    firstIndex: -1,
    //准备数据
    //数据结构：以一组一组来进行设定
    commodityAttr: [],
    attrValueList: [],
    //  顾客评价信息
    appraiseList: [],
    // 商品参数信息
    cahnshuList: [],
    modalType: 0,//弹窗类型
    addNum: 1,//加入购物车数量
    // 相似推荐
    recommendList: [],
    currentPage: 1,
    pageSize: 3,
  },

  // 弹窗
  setModalStatus: function (e) {
    var that = this;
    if (that.data.itemData.goods_number > 0){
      var animation = wx.createAnimation({
        duration: 200,
        timingFunction: "linear",
        delay: 0
      })

      this.animation = animation
      animation.translateY(300).step();

      this.setData({
        animationData: animation.export(),
        modalType: e.currentTarget.dataset.modaltype,
      })

      if (e.currentTarget.dataset.status == 1) {
        this.setData(
          {
            showModalStatus: true
          }
        );
      }
      setTimeout(function () {
        animation.translateY(0).step()
        this.setData({
          animationData: animation
        })
        if (e.currentTarget.dataset.status == 0) {
          this.setData(
            {
              showModalStatus: false
            }
          );
        }
      }.bind(this), 200)
    }else {
      that.setData({
        count: 1500,
        toastText: "库存不足",
        tips_icon: '/images/tips_icon3.png',
      })
      // that.showToast();
      that.dialog.showDialog();
    }
    
  },
  // 加减
  changeNum: function (e) {
    var that = this;
    if (that.data.modalType == 2) {//立即购买
      if (e.target.dataset.alphaBeta == 0) {
        if (this.data.buynum <= 1) {
          buynum: 1
        } else {
          this.setData({
            buynum: this.data.buynum - 1
          })
        };
      } else {
        if (that.data.buynum < that.data.itemData.goods_number) {
          this.setData({
            buynum: this.data.buynum + 1
          })
        }
      };
    } else if (that.data.modalType == 1) {//加入购物车
      if (e.target.dataset.alphaBeta == 0) {
        if (this.data.addNum <= 1) {
          addNum: 1
        } else {
          this.setData({
            addNum: this.data.addNum - 1
          })
        };
      } else {
        if (that.data.addNum < that.data.itemData.goods_number) {
          this.setData({
            addNum: this.data.addNum + 1
          })
        }
      };
    }
  },
  onReady:function(){
    //获得dialog组件
    this.dialog = this.selectComponent("#dialog");
  },
  // 传值
  onLoad: function (option) {
    //this.initNavHeight();
    var that = this;
    that.setData({
      productId: option.productId,
      UpOrDown: 0,
    });
    that.loadProductDetail();
    that.customerAppraise();
    that.adfrash();
  },
  // 跳转到详情页面
  todetail: function (e) {
    var goods_id = e.currentTarget.dataset.goodid;
    wx.navigateTo({
      url: "../product/detail?productId=" + goods_id
    })
  },
  // 相似推荐获取数据
  adfrash: function () {
    var that = this;
    app.request("WxGoodsController", "queryRecommend", { goods_id: that.data.productId, currentPage: that.data.currentPage, pageSize: that.data.pageSize }, function (data) {
      var list = data.recommendList.list;
      var total = data.recommendList.pagination.total;
      var size = data.recommendList.pagination.pageSize;
      var cupage = data.recommendList.pagination.current;
      that.setData({
        currentPage: that.data.currentPage + 1,
      });
      if (total < size * cupage) {
        that.setData({
          currentPage: 1,
        });
      } else if (total == size * cupage) {
        that.setData({
          currentPage: 1,
        });
      }
      that.setData({
        recommendList: list,
      });
    });
  },
  // 获取顾客评价
  customerAppraise: function () {
    var that = this;
    app.request("WxGoodsController", "queryCustomerAppraise", { goods_id: that.data.productId },
      function (data) {
        var list = data.appraiseList;
        for (let i = 0; i < list.length; i++) {
          if (list[i].user_picture == '' || list[i].user_picture == undefined) {
            list[i]['user_picture'] = that.data.imageUrlFix + "wechatShop/user_img.png";
          } else if (list[i].user_picture.indexOf('https') < 0) {
            list[i]['choose'] = 1;
          }
        }
        that.setData({
          appraiseList: list,
        });
      });
  },
  // 商品详情数据获取
  loadProductDetail: function () {
    var that = this;
    app.request("WxGoodsController", "queryGoodsDetail", { goods_id: that.data.productId, user_id: app.d.userId }, function (data) {
      var pro = data.pro;
      var content = pro.goods_desc;
      WxParse.wxParse('content', 'html', content, that, 3);
      that.setData({
        itemData: pro,
        bannerItem: pro.img_arr,
        cahnshuList: pro.chan_shu,
      });
      that.loadGoodsUserFavorites();
    });
    // wx.request({
    //   url: app.d.ceshiUrl + '/Api/Product/index',
    //   method:'post',
    //   data: {
    //     pro_id: that.data.productId,
    //   },
    //   header: {
    //     'Content-Type':  'application/x-www-form-urlencoded'
    //   },
    //   success: function (res) {
    //     //--init data 
    //     var status = res.data.status;
    //     if(status==1) {   
    //       var pro = res.data.pro;
    //       var content=pro.content;
    //       //that.initProductData(data);
    //       WxParse.wxParse('content', 'html', content, that, 3);
    //       that.setData({
    //         itemData:pro,
    //         bannerItem:pro.img_arr,
    //         commodityAttr:res.data.commodityAttr,
    //         attrValueList:res.data.attrValueList,
    //       });
    //     } else {
    //       wx.showToast({
    //         title:res.data.err,
    //         duration:2000,
    //       });
    //     }
    //   },
    //   error:function(e){
    //     wx.showToast({
    //       title:'网络异常！',
    //       duration:2000,
    //     });
    //   },
    // });
  },
  //获取收藏状态
  loadGoodsUserFavorites: function () {
    var that = this;
    app.request("UserFavoritesService", "queryGoodsUserFavorites", {
      uid: app.d.userId,
      pid: that.data.productId,
    }, function (data) {
      var favoritesGoods = data.favoritesGoods;
      if (favoritesGoods && favoritesGoods.length > 0) {
        var tmpItemData = that.data.itemData;
        tmpItemData["isCollect"] = true;
        that.setData({
          itemData: tmpItemData,
        })
      }
    });
  },

  
  // 属性选择
  onShow: function () {
    this.setData({
      includeGroup: this.data.commodityAttr
    });
    this.distachAttrValue(this.data.commodityAttr);
    // 只有一个属性组合的时候默认选中
    // console.log(this.data.attrValueList);
    if (this.data.commodityAttr.length == 1) {
      for (var i = 0; i < this.data.commodityAttr[0].attrValueList.length; i++) {
        this.data.attrValueList[i].selectedValue = this.data.commodityAttr[0].attrValueList[i].attrValue;
      }
      this.setData({
        attrValueList: this.data.attrValueList
      });
    }
  },
  /* 获取数据 */
  distachAttrValue: function (commodityAttr) {
    /**
      将后台返回的数据组合成类似
      {
        attrKey:'型号',
        attrValueList:['1','2','3']
      }
    */
    // 把数据对象的数据（视图使用），写到局部内
    var attrValueList = this.data.attrValueList;
    // 遍历获取的数据
    for (var i = 0; i < commodityAttr.length; i++) {
      for (var j = 0; j < commodityAttr[i].attrValueList.length; j++) {
        var attrIndex = this.getAttrIndex(commodityAttr[i].attrValueList[j].attrKey, attrValueList);
        // console.log('属性索引', attrIndex); 
        // 如果还没有属性索引为-1，此时新增属性并设置属性值数组的第一个值；索引大于等于0，表示已存在的属性名的位置
        if (attrIndex >= 0) {
          // 如果属性值数组中没有该值，push新值；否则不处理
          if (!this.isValueExist(commodityAttr[i].attrValueList[j].attrValue, attrValueList[attrIndex].attrValues)) {
            attrValueList[attrIndex].attrValues.push(commodityAttr[i].attrValueList[j].attrValue);
          }
        } else {
          attrValueList.push({
            attrKey: commodityAttr[i].attrValueList[j].attrKey,
            attrValues: [commodityAttr[i].attrValueList[j].attrValue]
          });
        }
      }
    }
    // console.log('result', attrValueList)
    for (var i = 0; i < attrValueList.length; i++) {
      for (var j = 0; j < attrValueList[i].attrValues.length; j++) {
        if (attrValueList[i].attrValueStatus) {
          attrValueList[i].attrValueStatus[j] = true;
        } else {
          attrValueList[i].attrValueStatus = [];
          attrValueList[i].attrValueStatus[j] = true;
        }
      }
    }
    this.setData({
      attrValueList: attrValueList
    });
  },
  getAttrIndex: function (attrName, attrValueList) {
    // 判断数组中的attrKey是否有该属性值
    for (var i = 0; i < attrValueList.length; i++) {
      if (attrName == attrValueList[i].attrKey) {
        break;
      }
    }
    return i < attrValueList.length ? i : -1;
  },
  isValueExist: function (value, valueArr) {
    // 判断是否已有属性值
    for (var i = 0; i < valueArr.length; i++) {
      if (valueArr[i] == value) {
        break;
      }
    }
    return i < valueArr.length;
  },
  /* 选择属性值事件 */
  selectAttrValue: function (e) {
    /*
    点选属性值，联动判断其他属性值是否可选
    {
      attrKey:'型号',
      attrValueList:['1','2','3'],
      selectedValue:'1',
      attrValueStatus:[true,true,true]
    }
    console.log(e.currentTarget.dataset);
    */
    var attrValueList = this.data.attrValueList;
    var index = e.currentTarget.dataset.index;//属性索引
    var key = e.currentTarget.dataset.key;
    var value = e.currentTarget.dataset.value;
    if (e.currentTarget.dataset.status || index == this.data.firstIndex) {
      if (e.currentTarget.dataset.selectedvalue == e.currentTarget.dataset.value) {
        // 取消选中
        this.disSelectValue(attrValueList, index, key, value);
      } else {
        // 选中
        this.selectValue(attrValueList, index, key, value);
      }

    }
  },
  /* 选中 */
  selectValue: function (attrValueList, index, key, value, unselectStatus) {
    // console.log('firstIndex', this.data.firstIndex);
    var includeGroup = [];
    if (index == this.data.firstIndex && !unselectStatus) { // 如果是第一个选中的属性值，则该属性所有值可选
      var commodityAttr = this.data.commodityAttr;
      // 其他选中的属性值全都置空
      // console.log('其他选中的属性值全都置空', index, this.data.firstIndex, !unselectStatus);
      for (var i = 0; i < attrValueList.length; i++) {
        for (var j = 0; j < attrValueList[i].attrValues.length; j++) {
          attrValueList[i].selectedValue = '';
        }
      }
    } else {
      var commodityAttr = this.data.includeGroup;
    }

    for (var i = 0; i < commodityAttr.length; i++) {
      for (var j = 0; j < commodityAttr[i].attrValueList.length; j++) {
        if (commodityAttr[i].attrValueList[j].attrKey == key && commodityAttr[i].attrValueList[j].attrValue == value) {
          includeGroup.push(commodityAttr[i]);
        }
      }
    }
    attrValueList[index].selectedValue = value;

    // 判断属性是否可选
    // for (var i = 0; i < attrValueList.length; i++) {
    //   for (var j = 0; j < attrValueList[i].attrValues.length; j++) {
    //     attrValueList[i].attrValueStatus[j] = false;
    //   }
    // }
    // for (var k = 0; k < attrValueList.length; k++) {
    //   for (var i = 0; i < includeGroup.length; i++) {
    //     for (var j = 0; j < includeGroup[i].attrValueList.length; j++) {
    //       if (attrValueList[k].attrKey == includeGroup[i].attrValueList[j].attrKey) {
    //         for (var m = 0; m < attrValueList[k].attrValues.length; m++) {
    //           if (attrValueList[k].attrValues[m] == includeGroup[i].attrValueList[j].attrValue) {
    //             attrValueList[k].attrValueStatus[m] = true;
    //           }
    //         }
    //       }
    //     }
    //   }
    // }
    // console.log('结果', attrValueList);
    this.setData({
      attrValueList: attrValueList,
      includeGroup: includeGroup
    });

    var count = 0;
    for (var i = 0; i < attrValueList.length; i++) {
      for (var j = 0; j < attrValueList[i].attrValues.length; j++) {
        if (attrValueList[i].selectedValue) {
          count++;
          break;
        }
      }
    }
    if (count < 2) {// 第一次选中，同属性的值都可选
      this.setData({
        firstIndex: index
      });
    } else {
      this.setData({
        firstIndex: -1
      });
    }
  },
  /* 取消选中 */
  disSelectValue: function (attrValueList, index, key, value) {
    var commodityAttr = this.data.commodityAttr;
    attrValueList[index].selectedValue = '';

    // 判断属性是否可选
    for (var i = 0; i < attrValueList.length; i++) {
      for (var j = 0; j < attrValueList[i].attrValues.length; j++) {
        attrValueList[i].attrValueStatus[j] = true;
      }
    }
    this.setData({
      includeGroup: commodityAttr,
      attrValueList: attrValueList
    });

    for (var i = 0; i < attrValueList.length; i++) {
      if (attrValueList[i].selectedValue) {
        this.selectValue(attrValueList, i, attrValueList[i].attrKey, attrValueList[i].selectedValue, true);
      }
    }
  },

  initProductData: function (data) {
    data["LunBoProductImageUrl"] = [];

    var imgs = data.LunBoProductImage.split(';');
    for (let url of imgs) {
      url && data["LunBoProductImageUrl"].push(app.d.hostImg + url);
    }

    data.Price = data.Price / 100;
    data.VedioImagePath = app.d.hostVideo + '/' + data.VedioImagePath;
    data.videoPath = app.d.hostVideo + '/' + data.videoPath;
  },

  //点击收藏按钮
  isFavorites: function (e) {
    var that = this;
    var is_collect = e.currentTarget.dataset.iscollect;
    if (is_collect) {
      that.deleteFavorites();
    } else {
      that.addFavorites();
    }
  },

  // 取消收藏
  deleteFavorites: function () {
    var that = this;
    if (that.data.itemData.isCollect) {
      app.request("UserFavoritesService", "deleteGoodsUserFavorites", {
        uid: app.d.userId,
        pid: that.data.productId,
      }, function (data) {
        wx: wx.showToast({
          title: '已取消收藏！',
          duration: 2000,
        })
        var tmpItemData = that.data.itemData;
        tmpItemData["isCollect"] = false;
        that.setData({
          itemData: tmpItemData,
        })
      });
    }
  },

  //添加到收藏
  addFavorites: function (e) {
    var that = this;
    if (that.data.itemData != null) {
      if (!that.data.itemData.isCollect) {
        app.request("UserFavoritesService", "addGoodsUserFavorites", {
          uid: app.d.userId,
          pid: that.data.productId,
        }, function (data) {
          wx.showToast({
            title: '收藏成功！',
            duration: 2000
          });
          //变成已收藏，但是目前小程序可能不能改变图片，只能改样式
          var tmpItemData = that.data.itemData;
          tmpItemData["isCollect"] = true;
          that.setData({
            itemData: tmpItemData,
          })
        });
      }
    } else {
      app.request("UserFavoritesService", "addGoodsUserFavorites", {
        uid: app.d.userId,
        pid: that.data.productId,
      }, function (data) {
        wx.showToast({
          title: '收藏成功！',
          duration: 2000
        });
        //变成已收藏，但是目前小程序可能不能改变图片，只能改样式
        var tmpItemData = that.data.itemData;
        tmpItemData["isCollect"] = true;
        that.setData({
          itemData: tmpItemData,
        })
      });
    }

    // wx.request({
    //   url: app.d.ceshiUrl + '/Api/Product/col',
    //   method:'post',
    //   data: {
    //     uid: app.d.userId,
    //     pid: that.data.productId,
    //   },
    //   header: {
    //     'Content-Type':  'application/x-www-form-urlencoded'
    //   },
    //   success: function (res) {
    //     // //--init data
    //     var data = res.data;
    //     if(data.status == 1){
    //       wx.showToast({
    //         title: '操作成功！',
    //         duration: 2000
    //       });
    //       //变成已收藏，但是目前小程序可能不能改变图片，只能改样式
    //       that.data.itemData.isCollect = true;
    //     }else{
    //       wx.showToast({
    //         title: data.err,
    //         duration: 2000
    //       });
    //     }
    //   },
    //   fail: function() {
    //     // fail
    //     wx.showToast({
    //       title: '网络异常！',
    //       duration: 2000
    //     });
    //   }
    // });
  },

  buynow: function (e) {
    var goodsId = e.currentTarget.dataset.goodsDetail.goods_id;
    console.log(goodsId);
    wx.redirectTo({
      url: '../order/pay?type=buynow&buynum=' + this.data.buynum + '&goodsId=' + goodsId,
    });
  },

  addShopCart: function (e) { //添加到购物车
    var that = this;
    app.request("WxOrderController", "addShopping", {
      uid: app.d.userId,
      pid: that.data.productId,
      num: that.data.addNum
    }, function (data) {
      if (data.cart_id) {
        var ptype = e.currentTarget.dataset.type;
        if (ptype == 'buynow') {
          wx.redirectTo({
            url: '../order/pay?cartId=' + data.cart_id
          });
          return;
        } else {
          wx.showToast({
            title: '加入购物车成功',
            icon: 'success',
            duration: 2000
          });
          that.setData({
            addNum: 1,
            showModalStatus: false,
          });
        }
      } else {
        that.setData({
          count: 1500,
          toastText: data.resultMsg,
          tips_icon: '/images/tips_icon3.png',
        });
        // that.showToast();
        that.dialog.showDialog();
      }
    });
    // wx.request({
    //   url: app.d.ceshiUrl + '/Api/Shopping/add',
    //   method:'post',
    //   data: {
    //     uid: app.d.userId,
    //     pid: that.data.productId,
    //     num: that.data.buynum,
    //   },
    //   header: {
    //     'Content-Type':  'application/x-www-form-urlencoded'
    //   },
    //   success: function (res) {
    //     // //--init data        
    //     var data = res.data;
    //     if(data.status == 1){
    //       var ptype = e.currentTarget.dataset.type;
    //       if(ptype == 'buynow'){
    //         wx.redirectTo({
    //           url: '../order/pay?cartId='+data.cart_id
    //         });
    //         return;
    //       }else{
    //         wx.showToast({
    //             title: '加入购物车成功',
    //             icon: 'success',
    //             duration: 2000
    //         });
    //       }     
    //     }else{
    //       wx.showToast({
    //             title: data.err,
    //             duration: 2000
    //         });
    //     }
    //   },
    //   fail: function() {
    //     // fail
    //     wx.showToast({
    //       title: '网络异常！',
    //       duration: 2000
    //     });
    //   }
    // });
  },
  // bindChange: function (e) {//滑动切换tab 
  //   var that = this;
  //   that.setData({ currentTab: e.detail.current });
  // },
  initNavHeight: function () {////获取系统信息
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
  },
  bannerClosed: function () {
    this.setData({
      bannerApp: false,
    })
  },
  swichNav: function (e) {//点击tab切换
    var that = this;
    if (that.data.currentTab === e.target.dataset.current) {
      return false;
    }
    else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },
  // outdown:function(e){
  //   var that = this;
  //   // console.log("11111111111111111", that.data.UporDown);
  //   that.setData({
  //      UporDown:1,
  //   });
  //   // console.log("111111111111111112", that.data.UporDown);
  // },
  // inup:function(e){
  //    var that = this;
  //   //  console.log("2222222222222222222", that.data.UporDown);
  //    that.setData({
  //      UporDown:0,
  //    });
  //   //  console.log("2222222222222222222", that.data.UporDown);
  // },
  // scrollHeight:function(e){
  //    var that = this;
  //    if(e.detail.deltaY>0){
  //      console.log("3333333333331", that.data.UporDown);
  //      that.setData({
  //        UporDown: 0,
  //      });
  //      console.log("3333333333332", that.data.UporDown);
  //    }
  // },
  onShareAppMessage: function () {
    var data = new Date();
    var shared = data.getTime() + "";
    var shareId = shared.substring(8, shared.length) + app.d.userId;
    return {
      title: '大肚商城',
      path: '/pages/index/index?duid=' + app.d.userId + "&shareId=" + shareId,
      success: function (res) {
        app.request("DistributorService", "shareLog", {
          uid: app.d.userId,
          shareId: shareId,
        }, function (data) {
          // 分享成功
          console.log(res);
        })
      },
      fail: function (res) {
        // 分享失败
        console.log(res);
      }
    }
  }
});
